גרף הבסיס

g <- 
ggplot(iris) +
  aes(
    x = Sepal.Length,
    y = Sepal.Width,
    color = Species
    ) +
  geom_point() +
  theme_classic()

g

שינוי מיקום בסיסי

למעלה

g +
  theme(
    legend.position = "top"
  )

למטה

g +
  theme(
    legend.position = "bottom"
  )

שמאל

g +
  theme(
    legend.position = "left"
  )

שינוי כותרת המקרא

g +
  theme(
    legend.position = "left"
  ) +
  labs(
    color = "hello"
  )

ללא כותרת כלל

g +
  theme(
    legend.position = "left"
  ) +
  labs(
    color = ""
  )

שינוי גודל החיווים

לא מוכן

#g +
#  theme(
#    legend.position = "left",
#    legend.key.height= unit(10, 'cm'),
#    legend.key.width= unit(10, 'cm')
#  ) +
#  labs(
#    color = ""
#  )
#

שינוי המקרא לאורך או לרוחב

לא מוכן

g +
  theme(
    legend.box = "vertical"
  ) +
  labs(
    color = ""
  )

g +
  theme(
    legend.box = "horizontal"
  ) +
  labs(
    color = ""
  )

שינוי גודל הטקסט

g +
  theme(
      legend.text = 
        element_text(
          size=20
          )
  ) +
  labs(
    color = ""
  )

שינוי המיקוןם של הטקסט

g +
  labs(
    color = ""
  ) +
  guides(
    color = 
      guide_legend(
        label.position = "top",
        nrow = 1
      )
    )

g +
  labs(
    color = ""
  ) +
  guides(
    color = 
      guide_legend(
        label.position = "top",
      )
    )

שינוי מיקום מורכב יותר

g +
  labs(
    color = ""
  ) +
  guides(
    color = 
      guide_legend(
        label.position = "top",
        nrow = 1
      )
    ) +
  theme(
    legend.justification = c("right", "top")
  )

g +
  labs(
    color = ""
  ) +
  guides(
    color = 
      guide_legend(
        label.position = "top",
        nrow = 1
      )
    ) +
  theme(
    legend.justification = c("bottom")
  )

g +
  labs(
    color = ""
  ) +
  guides(
    color = 
      guide_legend(
        label.position = "top",
        nrow = 1
      )
    ) +
  theme(
    legend.position = "top",
    legend.justification = c("left")
  )

g +
  labs(
    color = ""
  ) +
  guides(
    color = 
      guide_legend(
        label.position = "top",
        nrow = 1
      )
    ) +
  theme(
    legend.position = "top",
    legend.justification = c("left")
  )

מקרא כפול

gg <- 
ggplot(iris) +
  aes(
    x = Sepal.Length,
    y = Sepal.Width,
    color = Species,
    size = Sepal.Length
    ) +
  geom_point() +
  theme_classic()

gg

gg +
  labs(
    color = "",
    size = ""
  )

gg +
  labs(
    color = "",
    size = ""
  ) +
  
  guides(
    color = 
      guide_legend(
        label.position = "top",
        nrow = 1
      ),
    size = 
      guide_legend(
        label.position = "top",
        nrow = 1
      )
    ) +
  theme(
    legend.position = "right"
  )

gg +
  labs(
    color = "",
    size = ""
  ) +
  
  guides(
    color = 
      guide_legend(
        label.position = "top",
        nrow = 1
      ),
    size = 
      guide_legend(
        label.position = "top",
        nrow = 1
      )
    ) +
  theme(
    legend.position = "right",
    legend.box = "horizontal"
  )

gg +
  labs(
    color = "",
    size = ""
  ) +
  
  guides(
    color = 
      guide_legend(
        label.position = "top",
        nrow = 1
      ),
    size = 
      guide_legend(
        label.position = "top",
        nrow = 1
      )
    ) +
  theme(
    legend.position = "top",
    legend.box = "horizontal"
  )

gg +
  labs(
    color = "",
    size = ""
  ) +
  
  guides(
    color = 
      guide_legend(
        label.position = "top",
        nrow = 1
      ),
    size = 
      guide_legend(
        label.position = "top",
        nrow = 1
      )
    ) +
  theme(
    legend.position = "top",
    legend.justification = c("left"),
    legend.box = "horizontal"
  )

gg +
  labs(
    title = "title",
    subtitle = "subtitle",
    color = "color",
    size = "size"
  ) +
  
  guides(
    color = 
      guide_legend(
        label.position = "bottom",
        nrow = 1,
        title.position = "top"
      ),
    size = 
      guide_legend(
        label.position = "bottom",
        nrow = 1,
        title.position = "top"
      )
    ) +
  theme(
    legend.position = "top",
    legend.justification = c("right"),
    legend.box = "horizontal"
  )

gg +
  labs(
    title = "title",
    subtitle = "subtitle",
    color = "color",
    size = "size"
  ) +
  
  guides(
    color = 
      guide_legend(
        label.position = "bottom",
        nrow = 1,
        title.position = "top"
      ),
    size = 
      guide_legend(
        label.position = "bottom",
        nrow = 1,
        title.position = "top"
      )
    ) +
  theme(
    legend.position = c(0.99, 1.04),
    legend.justification = c("right"),
    legend.box = "horizontal"
  )

gg +
  labs(
    title = "title",
    subtitle = "subtitle",
    color = "",
    size = ""
  ) +
  
  guides(
    color = 
      guide_legend(
        label.position = "bottom",
        nrow = 1,
        title.position = "top"
      ),
    size = 
      guide_legend(
        label.position = "bottom",
        nrow = 1,
        title.position = "top"
      )
    ) +
  theme(
    legend.spacing = unit(0, "cm"),
    legend.margin = margin(),
    legend.position = c(1.01, 1.09),
    legend.justification = c("right"),
    legend.box = "horizontal"
  )

צבעים של הקופסא

g + 
  theme(
    legend.background = 
      element_rect(
        fill="lightblue", 
        size=0.5,
        linetype="solid"
        )
    )

g + 
  theme(
    legend.background = 
      element_rect(
        fill = "lightblue", 
        size = 0.5,
        linetype = "solid",
        colour = "darkblue"
        )
    )

gg + 
  theme(
    legend.box.background = element_rect(color="red", size=2),
    legend.box.margin = margin(6, 6, 6, 6)
)

gg + 
  theme(
    legend.key = 
      element_rect(
        fill = "springgreen",
        colour = "black"
        )
    )

סדר של הופעת מקראים כאשר יש כמה

בלי פקודת סדר:

gg +
  labs(
    title = "title",
    subtitle = "subtitle",
    color = "",
    size = ""
  ) +
  
  guides(
    color = 
      guide_legend(
        label.position = "bottom",
        nrow = 1,
        title.position = "top"
      ),
    size = 
      guide_legend(
        label.position = "bottom",
        nrow = 1,
        title.position = "top"
      )
    ) +
  theme(
    legend.position = c(1.01, 1.09),
    legend.justification = c("right"),
    legend.box = "horizontal"
  )

שינוי הסדר

gg +
  labs(
    title = "title",
    subtitle = "subtitle",
    color = "",
    size = ""
  ) +
  
  guides(
    color = 
      guide_legend(
        order = 1,
        label.position = "bottom",
        nrow = 1,
        title.position = "top"
      ),
    size = 
      guide_legend(
        order = 2,
        label.position = "bottom",
        nrow = 1,
        title.position = "top"
      )
    ) +
  theme(
    
    legend.position = c(1.01, 1.09),
    legend.justification = c("right"),
    legend.box = "horizontal"
  )

מחיקת מקראים מרובים

גרף:

gg 

מחיקת גודל

gg +
    guides(
    size = FALSE
    )

מתקדם